<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Lawyer as LawyerModel;
use think\Db;
use think\Request;


/**
 * 首页接口
 */
class Lawyer extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    public function getLawyerList()
    {
        // 获取分页参数
        $page = $this->request->post('page', 1);
        $pageSize = $this->request->post('pageSize', 20);

        // 查询条件
        $where = [];
        $where['is_delete'] = 0;

        $lawyer = new LawyerModel();
        $total = $lawyer->where($where)->count();
        $list = $lawyer
            ->where($where)
            ->order('id', 'desc')
            ->page($page, $pageSize)
            ->select();

        $this->success('请求成功', ['list' => $list, 'total' => $total]);
    }

    public function getAllList()
    {
        // 查询条件
        $where = [];
        $where['is_delete'] = 0;

        $lawyer = new LawyerModel();
        $list = $lawyer
            ->where($where)
            ->order('id', 'desc')
            ->select();

        $this->success('请求成功', ['list' => $list]);
    }

    public function createLawyer(){
        $data = $this->request->post();
        $lawyer = new LawyerModel();
        $lawyer->name = $data['name'];
        $lawyer->createtime = date('Y-m-d H:i:s');
        $lawyer->save();
        $this->success('创建成功');
    }

    public function updateLawyer()
    {
        $data = $this->request->post();

        if (empty($data['id'])) {
            $this->error('缺少ID');
        }

        $lawyer = new LawyerModel();
        if (!$lawyer) {
            $this->error('记录不存在');
        }

        $updateData=[
            'name'=>$data['name'],
            'updatetime'=>date('Y-m-d H:i:s')
        ];
        $res = $lawyer->where('id', $data['id'])->update($updateData);

        if ($res !== false) {
            $this->success('更新成功');
        } else {
            $this->error('更新失败');
        }
    }

    public function deleteMulty(){
        $ids = $this->request->post('ids', '');
        $ids=explode(',',$ids);
        $lawyer = new LawyerModel();
        $lawyer->whereIn('id',$ids)->update(['is_delete'=>1]);
        $this->success();
    }


    public function deleteLawyer(){
        $id = $this->request->post('id', '');
        $lawyer = new LawyerModel();
        $lawyer->whereIn('id',$id)->update(['is_delete'=>1]);
        $this->success();
    }



}
